import {addArtDao,delArtDao,getArtByIdDao,updateArtDao
} from "../Dao/articleDao.js"
import {getTypeByIdDao} from "../Dao/typeDao.js"
import {findUserByIdDao} from "../Dao/userDao.js"
import {getArtByPageDao} from "../Dao/articleDao.js"


export const addArtServ = async (artobj) => {
    await addArtDao(artobj)
    return {
        code: 0,
        msg: "add art"
    }
}

export const delArtServ = async (id) => {
    return await delArtDao(id)
}

export const updateArtServ = async (id, updateobj) => {
    return await updateArtDao(id, updateobj)
}


export const findArtByPageServ = async (page, size, queryobj) => {
    const res = await getArtByPageDao(page, size, queryobj)
    res.list = await Promise.all(res.list.map(async item => {
        const obj = item.toObject()
        obj.type = await getTypeByIdDao(item.typeId)
        obj.user = await findUserByIdDao(item.userId)
        return obj
    }))
    return {
        code:0,
        data:res
    }
}

export const findArtByIdServ = async (id) =>{
    const art =await getArtByIdDao(id);
    const data =art.toObject()
    data.user =await findUserByIdDao(data.userId)
    data.type =await getTypeByIdDao(data.typeId)
    return {
    code:0,
    data
    }
}


